package com.qk.controller;

import com.github.pagehelper.Page;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result listRoles(String name,
                            String label,
                            @RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "3") Integer pageSize){
        log.info("分页查询角色，参数：name={}，label={}，page={}，pageSize={}",name,label,page,pageSize);
        PageResult<Role> pageResult =roleService.listRoles(name,label,page,pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色，参数：id={}",id);
        roleService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("添加角色：{}",role);
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id: {}",id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role){
        log.info("修改角色：{}",role);
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有角色");
        List<Role> roles = roleService.getAll();
        return Result.success(roles);
    }

}
